package cn.itcast.mq.listen;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleMsg(String msg){
//      log.info("消费者接收到消息 ==> 【{}】",msg);

    private static long workNum1 = 0;
    private static long workNum2 = 0;

    @RabbitListener(queues = "simple.queue")
    public void listenWorkMsg01(String msg) throws InterruptedException {
        workNum1++;
        log.warn("消费者1接收到消息 ==> 【{}】 处理消息数量：{}",msg,workNum1);
        TimeUnit.MILLISECONDS.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkMsg02(String msg) throws InterruptedException {
        workNum2++;
        log.warn("消费者2接收到消息 ==> 【{}】处理消息数量：{}",msg,workNum2);
        TimeUnit.MILLISECONDS.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutMsg01(String msg) throws InterruptedException {
        workNum2++;
        log.warn("消费者1接收到fanout消息 ==> 【{}】",msg);
        TimeUnit.MILLISECONDS.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutMsg02(String msg) throws InterruptedException {
        workNum2++;
        log.warn("消费者2接收到fanout消息 ==> 【{}】",msg);
        TimeUnit.MILLISECONDS.sleep(200);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void listenDirectMsg01(String msg){
        log.warn("消费者1接收到Direct消息 ==> 【{}】",msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void listenDirectMsg02(String msg){
        log.warn("消费者2接收到Direct消息 ==> 【{}】",msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ))
    public void listenTopicMsg01(String msg){
        log.warn("消费者1接收到Topic消息 ==> 【{}】",msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ))
    public void listenTopicMsg02(String msg){
        log.warn("消费者2接收到Topic消息 ==> 【{}】",msg);
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectMsg02(Map map){

        log.warn("消费者1接收到fanout消息 ==> 【{}】",map);

    }

}
